package org.jeecg.modules.zcglRecipients.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.zcgl.entity.AssetFixed;
import org.jeecg.modules.zcgl.service.IAssetFixedService;
import org.jeecg.modules.zcglRecipients.entity.ZcglRecipients;
import org.jeecg.modules.zcglRecipients.entity.ZcglRecipientsAssets;
import org.jeecg.modules.zcglRecipients.mapper.ZcglRecipientsAssetsMapper;
import org.jeecg.modules.zcglRecipients.mapper.ZcglRecipientsMapper;
import org.jeecg.modules.zcglRecipients.service.IZcglRecipientsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 资产领用
 * @Author: jeecg-boot
 * @Date:   2022-10-24
 * @Version: V1.0
 */
@Service
public class ZcglRecipientsServiceImpl extends ServiceImpl<ZcglRecipientsMapper, ZcglRecipients> implements IZcglRecipientsService {

	@Autowired
	private ZcglRecipientsMapper zcglRecipientsMapper;

	@Autowired
	private IAssetFixedService iAssetFixedService;


	@Autowired
	private ZcglRecipientsAssetsMapper zcglRecipientsAssetsMapper;

	@Override
	public int totayCount() {
		return zcglRecipientsMapper.totayCount();
	}

	@Override
	public List<ZcglRecipients> listByAsset(Map map) {
		return zcglRecipientsMapper.listByAsset(map);
	}

	@Override
	@Transactional
	public String saveMain(ZcglRecipients zcglRecipients, List<ZcglRecipientsAssets> zcglRecipientsAssetsList) {
		zcglRecipientsMapper.insert(zcglRecipients);
		if(zcglRecipientsAssetsList!=null && zcglRecipientsAssetsList.size()>0) {
			for(ZcglRecipientsAssets entity:zcglRecipientsAssetsList) {
				//外键设置
				entity.setZcglRecipientsId(zcglRecipients.getId());
                 //增加领用时增加单价
		        AssetFixed assetFixed = iAssetFixedService.getOne(new QueryWrapper<AssetFixed>().eq("id", entity.getAssetId()).eq("del_flag", 0));
				entity.setAssetPrice(assetFixed.getPrice());

				zcglRecipientsAssetsMapper.insert(entity);
			}
		}
		return  zcglRecipients.getId();
	}

	@Override
	@Transactional
	public void updateMain(ZcglRecipients zcglRecipients,List<ZcglRecipientsAssets> zcglRecipientsAssetsList) {
		zcglRecipientsMapper.updateById(zcglRecipients);

		//1.先删除子表数据
		zcglRecipientsAssetsMapper.deleteByMainId(zcglRecipients.getId());

		//2.子表数据重新插入
		if(zcglRecipientsAssetsList!=null && zcglRecipientsAssetsList.size()>0) {
			for(ZcglRecipientsAssets entity:zcglRecipientsAssetsList) {
				//外键设置
				entity.setZcglRecipientsId(zcglRecipients.getId());
				zcglRecipientsAssetsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		zcglRecipientsAssetsMapper.deleteByMainId(id);
		zcglRecipientsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			zcglRecipientsAssetsMapper.deleteByMainId(id.toString());
			zcglRecipientsMapper.deleteById(id);
		}
	}

}
